package com.example.holes;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;

public class Sky {
	Paint paint = new Paint();
	Bitmap sun;
	private int ANIMATE_TIME = 3 * Global.UPS; // time in SECONDS
	private int count = 0;
	private boolean sunsetStart = false, sunriseEnd = false;
	// sky bg
	private float R, G, B;
	private final float dayR, dayG, dayB;
	private final float nightR, nightG, nightB;
	private final float diffR, diffG, diffB;
	private final int left, top, right, bottom;
	// sun
	private final float leftMid, topMid, leftStart, topStart;
	private float sunLeft, sunTop;
	private final float leftInc;
	private final float topDec1, topDec2, topDec3, topDec4;
	private final float topInc1, topInc2, topInc3, topInc4;
	private final float one4th, two4ths, three4ths;
	
	public Sky(Bitmap image){
		// SKY color and position
		dayR = 120; dayG = 192; dayB = 168;
		nightR = 51; nightG = 51; nightB = 45;
		diffR = (dayR - nightR) / ANIMATE_TIME;
		diffG = (dayG - nightG) / ANIMATE_TIME;
		diffB = (dayB - nightB) / ANIMATE_TIME;
		left = 0; top = 0;
		right = Global.WIDTH;
		bottom = (int)(125 * Global.SCALE_RATIO_H);
		// SUN mid start point and beginngin start point
		sun = image;
		leftMid = 420 * Global.SCALE_RATIO_W;
		topMid = 9 * Global.SCALE_RATIO_H;
		leftStart = 220 * Global.SCALE_RATIO_W;
		topStart = 109 * Global.SCALE_RATIO_H;
		// sun X position increment
		leftInc = (float)200*Global.SCALE_RATIO_W / ANIMATE_TIME;
		// sun Y position decrement (FOR BEGGINING--SETTING)
		topDec1 = (float)66*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		topDec2 = (float)22*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		topDec3 = (float)8*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		topDec4 = (float)4*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		// sun Y position increment (FOR ENDING--RISING)
		topInc1 = (float)4*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		topInc2 = (float)8*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		topInc3 = (float)25*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		topInc4 = (float)75*Global.SCALE_RATIO_H/((float)ANIMATE_TIME/4);
		// divisions for the animation
		one4th = ((float)ANIMATE_TIME/4);
		two4ths = ((float)ANIMATE_TIME/4) * 2;
		three4ths = ((float)ANIMATE_TIME/4) * 3;
		paint.setARGB(255, (int)dayR, (int)dayG, (int)dayB);
	}
	
	// =========================
	//        v  UPDATE  v
	// =========================
	public void Update(){
		if (sunsetStart){
			count++;
			if (count < ANIMATE_TIME){
				R -= diffR;
				G -= diffG;
				B -= diffB;
				paint.setARGB(255, (int)R, (int)G, (int)B);
				sunLeft +=  leftInc;
				if (count < one4th)
					sunTop += topInc1;
				else if (count < two4ths)
					sunTop += topInc2;
				else if (count < three4ths)
					sunTop += topInc3;
				else
					sunTop += topInc4;
			}
			else
				sunsetStart = false;
		}
		else if (sunriseEnd){
			count++;
			if (count < ANIMATE_TIME){
				R += diffR;
				G += diffG;
				B += diffB;
				paint.setARGB(255, (int)R, (int)G, (int)B);
				sunLeft += leftInc;
				if (count < one4th)
					sunTop -= topDec1;
				else if (count < two4ths)
					sunTop -= topDec2;
				else if (count < three4ths)
					sunTop -= topDec3;
				else
					sunTop -= topDec4;
			}
			else
				sunriseEnd = false;
		}
	}
	// =========================
	//        ^  UPDATE  ^
	// =========================
	
	// =========================
	//         v  DRAW  v
	// =========================
	public void Draw(Canvas c){							// DRAW ORDER
		c.drawRect(left, top, right, bottom, paint);	// 1. sky
		if (sunsetStart){
			c.drawBitmap(sun, sunLeft, sunTop, null);	// 2a. sunset
		}
		else if (sunriseEnd){
			c.drawBitmap(sun, sunLeft, sunTop, null);	// 2b. sunrise
		}
	}
	// =========================
	//         ^  DRAW  ^
	// =========================
	
	public void startingAnimation(){
		sunLeft = leftMid;
		sunTop = topMid;
		R = dayR;
		G = dayG;
		B = dayB;
		count = 0;
		sunsetStart = true;
	}
	
	public void endingAnimation(){
		sunLeft = leftStart;
		sunTop = topStart;
		R = nightR;
		G = nightG;
		B = nightB;
		count = 0;
		sunriseEnd = true;
	}
	
	public boolean startAnimDone(){ return (sunsetStart == false); }
	public boolean endAnimDone(){ return (sunriseEnd == false); }
}
